<?php
error_reporting(E_ALL & ~E_NOTICE);
include 'config.php';
include 'scripts/class_image.php';
include 'scripts/function.php';
//print_r($_REQUEST);exit;
$watermarkid=intval($_REQUEST['watermark']);
$logoid=intval($_REQUEST['logo']);
$resizeid=intval($_REQUEST['resize']);
##################### START CONFIG #######################

$logolist = array(
	1 => 'logo1.png', 
	2 => 'logo2.png',
	3 => 'logo3.png',
);

// Nếu logo yêu cầu ko có trong danh sách thì dùng logo1.png 
$default['logo'] = 'logo1.png';


// kích cỡ resize
$resizelist = array(
	0	=> 0, // ko resize
	1	=> 640, 
	2	=> 800
);
$default['resize'] = 800;

##################### END CONFIG #######################

$watermark = $watermarkid > 0 ? TRUE : FALSE;

$logoPath = DIR . 'logo/' . (in_array($logoid, array_keys($logolist)) ? $logolist[$logoid] : $default['logo']);

$resizeWidth = in_array($resizeid, array_keys($resizelist)) ? $resizelist[$resizeid] : $default['resize'];

if (!empty($_FILES)) {
	$file = $_FILES['Filedata'];
	$imagePath = check_filename($upload['path'] . $setting['FileNamePrefix'].basename(convert_filename($file['name'])));

	
	if(check_filetype(laydinhdang($imagePath)))
	{
		if($file['size']<$setting['maxsize'])
		{	
			if(@move_uploaded_file($file['tmp_name'], $imagePath)) {
				if($resizeWidth > 0)
					c_Image::resize($imagePath, $resizeWidth, 0);
				if($watermark)
					c_Image::watermark($imagePath, $logoPath, $setting['PosWaterMark']);
				echo $upload['url'].basename($imagePath);
			}
			else
			{
				echo "loi|Không chuyển đc file tạm trên server.";
			}
		}
		else
		{
			echo "loi|File: {$file["name"]} Có Size là ".doidungluong($file["size"])." \nGiới hạn chỉ là ".doidungluong($setting['maxsize'])."";
		}
	}
	else
	{
		echo "loi|File: {$file["name"]} không cho phép upload (".laydinhdang($imagePath).")";
	}
}
if(isset($_POST['url']))
{
	if($url = trim($_POST['url']))
	{
		$imagePath = check_filename($upload['path'] . $setting['FileNamePrefix'].basename(convert_filename($url)));
		
		c_Image::leech($url, $imagePath);
		if($resizeWidth > 0)
			c_Image::resize($imagePath, $resizeWidth, 0);
		if($watermark)
			c_Image::watermark($imagePath, $logoPath, $setting['PosWaterMark']);
		echo $upload['url'].basename($imagePath);
	}
}
?>